package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 新增床位
     * @param dto
     */
    @Override
    public int addBed(BedDto dto) {
        //创建bed对象
        Bed bed = new Bed();
        //将dto中的数据拷贝给Bed对象
        BeanUtil.copyProperties(dto, bed);
        //将缺失的数据补充完整
        //设置状态
        bed.setBedStatus(0);
        //设置时间
        LocalDateTime now = LocalDateTime.now();
        bed.setCreateTime(now);
        bed.setUpdateTime(now);
        //设置人  应该是当前登录用户的ID
        //TODO: 创建人和修改人
        bed.setCreateBy(1L);
        bed.setUpdateBy(1L);
        //调用Mapper
        return bedMapper.addBed(bed);
    }

    @Override
    public BedVo findBed(Long id) {
        Bed bed = bedMapper.selectById(id);
        BedVo vo = new BedVo();
        BeanUtil.copyProperties(bed,vo);
        return vo;
    }

    @Override
    public void updateBed(BedDto dto) {
        if(dto==null){
            throw new BaseException(BasicEnum.BED_UPDATE_FAIL);
        }
        //创建bed对象
        Bed bed = new Bed();
        //将缺失的数据补充完整
        //设置状态
        bed.setBedStatus(0);
        //设置时间
        LocalDateTime now = LocalDateTime.now();
        bed.setUpdateTime(now);
        //设置人  应该是当前登录用户的ID
        //TODO: 修改人
        bed.setUpdateBy(1L);
        BeanUtil.copyProperties(dto,bed);
        bedMapper.updateBed(bed);
    }

    @Override
    public void deleteBed(Long id) {
        //删除判断
        Bed bed = bedMapper.selectById(id);
        if(bed.getBedStatus()!=0){
            //不能删
            throw new BaseException(BasicEnum.BED_DELETE_FAIL);
        }else {
            bedMapper.deleteBed(id);
        }
    }
}

